package com.scrawler.regex.template;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scrawler.main.InitProject;
import com.scrawler.util.FileUtil;
import com.scrawler.util.HttpUtils;
import com.scrawler.util.PathUtil;

@Service
public class ContentRegexTest {
	
	@Autowired
	private HttpUtils httpUtils;
	
	public static void main(String args[]) { 
	InitProject.init();
	ContentRegexTest nbaPage = InitProject.getContext().getBean(ContentRegexTest.class);
	String url="http://sports.ifeng.com/lanqiu/nba/lianmeng/detail_2013_07/30/28070868_0.shtml";
	// nbaPage.getContent(url,"utf-8")
    List resultList = getContext(FileUtil.readFile(PathUtil.getInstance().getBasePath()+"/demoData/ifengContent.txt"));
    for (Iterator iterator = resultList.iterator(); iterator.hasNext();) {
    	String context = (String) iterator.next();
       // System.out.println(context);
    }
}
	
	public  String getContent(String url,String encoding){
		return httpUtils.getContentByUrl(url,encoding);
	}
	
/**
 * 提取"<title>XXXX</title>"中的文字XXXX
 * @param html 要解析的html文档内容
 * @return 解析结果，可以多次匹配，每次匹配的结果按文档中出现的先后顺序添加进结果List
 */
public static List getContext(String html) {
	//FileUtil.writeStringToDisk("d:\\ifengContent.txt",html);
    List resultList = new ArrayList();
    String titleRegex="<h1 id=\"artical_topic\">(.*?)</h1>";
    String pubTimeRegex = "<span>(\\d{4}年\\d{2}月\\d{2}日 \\d{2}:\\d{2})</span>";
    String sourceRegex="来源：(.*?)</p>";
    String canyuRegex="<span id=\"comment_countall\" style=\"margin:0px;\">(.*?)</span>";
    String commentRegex="<span id=\"comment_count\" style=\"margin-right:0px;\">(.*?)</span>";
    String contentRegex="<div id=\"main_content\">(.*?)<!--mainContent end-->";
    List<String> regexs = new ArrayList<String>();
    regexs.add(titleRegex);
    regexs.add(pubTimeRegex);
    regexs.add(sourceRegex);
    regexs.add(canyuRegex);
    regexs.add(commentRegex);
    regexs.add(contentRegex);
    // 这一行非常重要,把多个空格换成一个空格
    html = html.replaceAll("\\s+"," ");
    System.out.println(html);
    
    for(String regex : regexs){
    	  Pattern title = Pattern.compile(regex);
    	  Matcher titleM = title.matcher(html);//开始编译
    	  System.out.println(regex);
    	  while (titleM.find()) {
    	    	String url =titleM.group(1);
    	        resultList.add(url);//获取被匹配的部分
    	        System.out.println("url: "+url);
    	    }
    }
    return resultList;
} 
	
}
